package henu.graduation_design.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

import henu.graduation_design.onlineshop.ProductListActivity;
import henu.graduation_design.onlineshop.R;
import henu.graduation_design.util.AsyncImageLoader;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CatalogAdapter extends BaseAdapter {

	private Context context;
	private List<HashMap<String, Object>> list;
	private String[] colors = new String[] { "#83AF9B", "#DBCFCA", "#A0BF7C",
			"#458994" };
	Random random;
	private AsyncImageLoader asyncImageLoader = new AsyncImageLoader();

	public CatalogAdapter(Context context, List<HashMap<String, Object>> list) {
		this.context = context;
		this.list = list;
		random = new Random();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final CatalogHolder holder;
		if (convertView == null) {
			View view = LayoutInflater.from(context).inflate(
					R.layout.gridview_catalog_cell, null);
			holder = new CatalogHolder();
			holder.imageView = (ImageView) view
					.findViewById(R.id.catalog_cell_image);
			holder.titleTextView = (TextView) view
					.findViewById(R.id.catalog_cell_title);
			holder.contentTextView = (TextView) view
					.findViewById(R.id.catalog_cell_content);
			holder.layout = (LinearLayout) view
					.findViewById(R.id.catalog_cell_layout);
			view.setTag(holder);
			convertView = view;
		} else {
			holder = (CatalogHolder) convertView.getTag();
		}
		final HashMap<String, Object> currentItem = list.get(position);

		holder.layout.setBackgroundColor(Color.parseColor(colors[random
				.nextInt(4)]));
		holder.titleTextView.setText(currentItem.get("title").toString());
		holder.contentTextView.setText(currentItem.get("content").toString());
		loadImage(currentItem.get("imageUri").toString(), holder.imageView);
		holder.layout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(context, ProductListActivity.class);
				intent.putExtra("ID", currentItem.get("id").toString());
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	private void loadImage(final String url, final ImageView imageView) {
		// 如果缓存过就会从缓存中取出图像，ImageCallback接口中方法也不会被执行
		Drawable cacheImage = asyncImageLoader.loadDrawable(url,
				new AsyncImageLoader.ImageCallback() {
					// 请参见实现：如果第一次加载url时下面方法会执行
					public void imageLoaded(Drawable imageDrawable) {
						imageView.setImageDrawable(imageDrawable);
					}
				});
		if (cacheImage != null) {
			imageView.setImageDrawable(cacheImage);
		}
	}

	private class CatalogHolder {
		private LinearLayout layout;
		private ImageView imageView;
		private TextView titleTextView;
		private TextView contentTextView;
	}

}
